package com.ksyzt.ksite2.client.news;

import org.mortbay.log.Log;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.MessageComposite;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.gwt.client.mainframe.AppData;
import com.ksyzt.gwt.client.mainframe.IAppModule;
import com.ksyzt.gwt.client.mainframe.ModuleProperties;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.modle.COLUMNObj;
import com.ksyzt.ksite2.shared.modle.VW_NEWSObj;

public class DraftFrame extends MessageComposite implements IAppModule
{

    @Override
    public void onInitialize(AppData application, ModuleProperties pros)
    {
	handler = this.addMessageHandler(application.messageHandler);
	initData(application.user.id);
    }

    @Override
    public void onUnInitialize()
    {
	handler.removeHandler();
    }

    @Override
    public Widget getRootWidget()
    {
	return this;
    }

    int m_userid;
    HandlerRegistration handler;

    private static DraftFrameUiBinder uiBinder = GWT
	    .create(DraftFrameUiBinder.class);

    interface DraftFrameUiBinder extends UiBinder<Widget, DraftFrame>
    {
    }

    public DraftFrame()
    {
	initWidget(uiBinder.createAndBindUi(this));

    }

    NewsList m_list;
    NewsEditor m_editor;

    Widget current;

    @UiField
    DockLayoutPanel rootScroller;

    /**
     * 草稿箱栏目信息
     */
    COLUMNObj m_column;
    /**
     * 获取草稿想栏目信息
     */
    private AsyncCallback<COLUMNObj> mColumnHandler = new AsyncCallback<COLUMNObj>()
    {

	@Override
	public void onSuccess(COLUMNObj result)
	{
	    m_column = result;
	    sureList();
	    m_list.initList(m_column);
	}

	@Override
	public void onFailure(Throwable caught)
	{
	    Window.alert(caught.getMessage());
	}
    };

    private void sureList()
    {
	if (m_list == null)
	{
	    m_list = new NewsList();
	    m_list.setUserId(m_userid);
	    m_list.addMessageHandler(m_list_handler);
	}
	if (!m_list.isAttached())
	{
	    if (current != null)
	    {
		current.removeFromParent();
	    }

	    rootScroller.add(m_list);
	    current = m_list;
	}
    }

    private void sureEditor()
    {
	if (m_editor == null)
	{
	    m_editor = new NewsEditor();
	    m_editor.setUserId(m_userid);
	    m_editor.addMessageHandler(m_editor_handler);
	}
	if (!m_editor.isAttached())
	{
	    if (current != null)
	    {
		current.removeFromParent();
	    }

	    rootScroller.add(m_editor);
	    current = m_editor;
	}
    }

    /**
     * 草稿想栏目ID
     */
    final static int COLUMN_DRAFT_ID = 142;

    public void initData(int mUserid)
    {
	m_userid = mUserid;
	ServerProxy.SYS_MANAGER.getColumn(COLUMN_DRAFT_ID, mColumnHandler);

    }

    private MessageHandler m_list_handler = new MessageHandler()
    {

	public void onMessage(Object sender, Integer message, Object value)
	{
	    if (MessageEvent.ITEMCLICK.equals(message))
	    {
		VW_NEWSObj n = (VW_NEWSObj) value;
		sureEditor();
		m_editor.edit(n, m_column.getID());
		m_editor.enableReturn(true);
	    } else if (MessageEvent.REFRESH.equals(message))
	    {

	    }
	}
    };
    private MessageHandler m_editor_handler = new MessageHandler()
    {

	public void onMessage(Object sender, Integer message, Object value)
	{

	    if (MessageEvent.RETURN == message)
	    {
		sureList();

		m_list.initList(m_column);

	    } else if (MessageEvent.MESSAGE == message)
	    {
		message((String) value);
	    }
	}
    };

}
